﻿using APESM_GGJ.CommonClass;
using MC030106_CI.ServiceReference1;
using Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Xml;
using Tools;

namespace UserPush
{
    class Program
    {
        static void Main(string[] args)
        {
            DBHelper db = null;
            try
            {
              
                String DATATYPE=ConfigurationManager.AppSettings["DATATYPE"].ToString();

                if (DATATYPE.Equals("2"))
                {

                    #region 销售用户
                    LogHelper.WriteLogInfo(typeof(Program), "获取销售用户开始");
                    
                    DataSet ds = getData(1);
                    
                    LogHelper.WriteLogInfo(typeof(Program), "获取销售用户结束");
                    if (ds != null)
                    {
                        LogHelper.WriteLogInfo(typeof(Program), "getuserend-usercount:" + ds.Tables[0].Rows.Count);
                        String USERID = null;
                        String CARVIN = null;
                        String OWNERSID = null;
                        String SYSID = null;
                        String msg = null;
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {

                            SYSID = row["SYSID"].ToString();
                            USERID = row["USERID"].ToString();
                            CARVIN = row["CARVIN"].ToString();
                            OWNERSID= row["OWNERSID"].ToString();
                            msg = String.Format("CARVIN:{0} USERID:{1} OWNERSID:{2} SYSID:{3}", CARVIN, USERID, OWNERSID, SYSID);
                            LogHelper.WriteLogInfo(typeof(Program), msg);

                            try
                            {

                                SPML model = new SPML();
                                model.MESSAGEID = "GB00002";
                                model.PUSH_TYPE = "10";
                                model.VIN = CARVIN;
                                model.USER_GUID = "0";
                                model.PUSH_CONTENT = ConfigurationManager.AppSettings["PUSH_CONTENT"].ToString();
                                model.PUSH_FOR = "2";
                                model.EXTRA_INFO = "";
                                string xml = "";
                                xml = objtoxml.XmlSerialize(model);
                                F_GB00002SoapClient ws = new F_GB00002SoapClient();
                                LogHelper.WriteLogInfo(typeof(Program), "postXML：" + xml);
                                string html = ws.InformationPush(xml);
                                LogHelper.WriteLogInfo(typeof(Program), "post结果：" + html);
                                if (html.Contains("070000"))
                                {
                                    db = new DBHelper();
                                    String sql = String.Format("update QN_USER set ISPUSH=1,LASTUPDATETIME=sysdate WHERE  SYSID='{0}' AND USERTYPE=1 ", SYSID);
                                    int ret = db.RunSql_Insert(sql);
                                    if (ret > 0)
                                    {

                                        LogHelper.WriteLogInfo(typeof(Program), "更新成功:" + msg);
                                  
                                    }
                                    else
                                    {
                                        LogHelper.WriteLogInfo(typeof(Program), "更新失败:" + msg);
                                    }

                                }
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLog(typeof(Program), ex);
                            }
                            finally
                            {
                                if (db != null)
                                {
                                    db.con.Close();
                                }
                            }

                        }


                    }
                    else {
                        LogHelper.WriteLogInfo(typeof(Program), "未获取到可执行的销售用户数据");
                    }
                    #endregion

                    #region 售后用户
                    LogHelper.WriteLogInfo(typeof(Program), "获取售后用户开始");
                    
                    ds = getData(2);
                   
                    LogHelper.WriteLogInfo(typeof(Program), "获取售后用户结束");                  

                    if (ds != null)
                    {
                        LogHelper.WriteLogInfo(typeof(Program), "getuserend-usercount:" + ds.Tables[0].Rows.Count);
                        String USERID = null;
                        String CARVIN = null;
                        String OWNERSID = null;
                        String SYSID = null;
                        String msg = null;
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            SYSID = row["SYSID"].ToString();
                            USERID = row["USERID"].ToString();
                            CARVIN = row["CARVIN"].ToString();
                            OWNERSID = row["OWNERSID"].ToString();
                            msg = String.Format("CARVIN:{0} USERID:{1} OWNERSID:{2} SYSID:{3}", CARVIN, USERID, OWNERSID, SYSID);
                            LogHelper.WriteLogInfo(typeof(Program), msg);

                            try
                            {

                                SPML model = new SPML();
                                model.MESSAGEID = "GB00002";
                                model.PUSH_TYPE = "11";
                                model.VIN = CARVIN;
                                model.USER_GUID = USERID;
                                model.PUSH_CONTENT = ConfigurationManager.AppSettings["PUSH_CONTENT"].ToString();
                                if (USERID == null || USERID.Trim().Length == 0)
                                {
                                    model.PUSH_FOR = "0";
                                    model.USER_GUID = "0";
                                }
                                else {
                                    model.PUSH_FOR = "1";
                                   
                                }
                                model.EXTRA_INFO = "";
                                string xml = "";
                                xml = objtoxml.XmlSerialize(model);
                                F_GB00002SoapClient ws = new F_GB00002SoapClient();
                                LogHelper.WriteLogInfo(typeof(Program), "postXML：" + xml);
                                string html = ws.InformationPush(xml);
                                LogHelper.WriteLogInfo(typeof(Program), "post结果：" + html);
                                if (html.Contains("070000"))
                                {
                                    db = new DBHelper();
                                    String sql = String.Format("update QN_USER set ISPUSH=1,LASTUPDATETIME=sysdate WHERE  SYSID='{0}' AND USERTYPE=2 ", SYSID);
                                    int ret = db.RunSql_Insert(sql);
                                    if (ret > 0)
                                    {
                                        LogHelper.WriteLogInfo(typeof(Program), "更新成功:" + msg);
                                      
                                    }
                                    else {
                                        LogHelper.WriteLogInfo(typeof(Program), "更新失败:" + msg);
                                    }

                                }
                            }
                            catch (Exception ex)
                            {
                                LogHelper.WriteLog(typeof(Program), ex);
                            }
                            finally
                            {
                                if (db != null)
                                {
                                    db.con.Close();
                                }
                            }

                        }


                    }
                    else
                    {
                        LogHelper.WriteLogInfo(typeof(Program), "未获取到可执行的售后用户数据");
                    }
                    #endregion
                }
                else if (DATATYPE.Equals("1")) {
                    try
                    {

                        SPML model = new SPML();
                        model.MESSAGEID = ConfigurationManager.AppSettings["MESSAGEID"].ToString();
                        model.PUSH_TYPE = ConfigurationManager.AppSettings["PUSH_TYPE"].ToString();
                        model.VIN = ConfigurationManager.AppSettings["VIN"].ToString(); ;
                        model.USER_GUID = ConfigurationManager.AppSettings["USER_GUID"].ToString();
                        model.PUSH_CONTENT = ConfigurationManager.AppSettings["PUSH_CONTENT"].ToString();
                        model.PUSH_FOR = ConfigurationManager.AppSettings["PUSH_FOR"].ToString();
                        model.EXTRA_INFO = ConfigurationManager.AppSettings["EXTRA_INFO"].ToString();
                        string xml = "";
                        xml = objtoxml.XmlSerialize(model);
                        F_GB00002SoapClient ws = new F_GB00002SoapClient();
                        LogHelper.WriteLogInfo(typeof(Program), "postXML：" + xml);
                        string html = ws.InformationPush(xml);
                        LogHelper.WriteLogInfo(typeof(Program), "post结果：" + html);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog(typeof(Program), ex);
                    }
                    finally
                    {
                        LogHelper.WriteLogInfo(typeof(Program), "测试执行完成");
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(typeof(Program), ex);
            }
            finally
            {
                LogHelper.WriteLogInfo(typeof(Program), "执行完成");
            }
          
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pushType">销售(售前)：1 入场（售后）：2</param>
        /// <returns></returns>
        private static DataSet getData(int pushType)
        {
            DBHelper db = new DBHelper();
            StringBuilder sbsql = new StringBuilder();
            sbsql.AppendFormat("SELECT USERID,CARVIN,OWNERSID,SYSID FROM QN_USER WHERE USERTYPE={0} and ISPUSH={1} and CREATEDATE>trunc(sysdate)", pushType,0);
            DataSet ds = db.RunSql_Select(sbsql.ToString());
            sbsql.Clear();
            db.con.Close();
            return ds;
        }
        private static int insertQNINFO_MY(String SYSID, int pushType)
        {
            DBHelper db = new DBHelper();
            StringBuilder sbsql = new StringBuilder();
            sbsql.AppendFormat("insert into QN_USER(SYSID,QNID,STATE) values('{0}',{1},{2})", SYSID, pushType,1);
            int ret= db.RunSql_Insert(sbsql.ToString());
            sbsql.Clear();
            db.con.Close();
            return ret;
        }




    }
}
